
package radio_station7.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;


@Entity
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Song.findAll", query = "SELECT s FROM Song s"),
    @NamedQuery(name = "Song.findById", query = "SELECT s FROM Song s WHERE s.id = :id"),
    @NamedQuery(name = "Song.findByTitle", query = "SELECT s FROM Song s WHERE s.title = :title"),
    @NamedQuery(name = "Song.findByDuration", query = "SELECT s FROM Song s WHERE s.duration = :duration"),
    @NamedQuery(name = "Song.findByTrackNr", query = "SELECT s FROM Song s WHERE s.trackNr = :trackNr"),
    @NamedQuery(name = "Song.findByPattern", query = "SELECT s FROM Song s LEFT OUTER JOIN TREAT(s.albumId AS ArtistAlbum) aa LEFT OUTER JOIN aa.artistId a LEFT OUTER JOIN TREAT(s.albumId AS BandAlbum) ba LEFT OUTER JOIN ba.bandId b WHERE LOWER(s.title) LIKE LOWER(:pattern) OR LOWER(aa.title) LIKE LOWER(:pattern) OR LOWER(a.firstName) LIKE LOWER(:pattern) OR LOWER(a.lastName) LIKE LOWER(:pattern) OR LOWER(ba.title) LIKE LOWER(:pattern) OR LOWER(b.name) LIKE LOWER(:pattern)")})
public class Song implements Serializable {
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(nullable = false)
    private Long id;
    @Basic(optional = false)
    @Column(nullable = false, length = 255)
    private String title;
    @Basic(optional = false)
    @Column(nullable = false)
    @Temporal(TemporalType.TIME)
    private Date duration;
    @Basic(optional = false)
    @Column(name = "TRACK_NR", nullable = false)
    private int trackNr;
    @ManyToMany(mappedBy = "songCollection")
    private Collection<Playlist> playlistCollection;
    @JoinColumn(name = "ALBUM_ID", referencedColumnName = "ID", nullable = false)
    @ManyToOne(optional = false)
    private Album albumId;

    public Song() {
    }

    public Song(Long id) {
        this.id = id;
    }

    public Song(Long id, String title, Date duration, int trackNr) {
        this.id = id;
        this.title = title;
        this.duration = duration;
        this.trackNr = trackNr;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        Long oldId = this.id;
        this.id = id;
        changeSupport.firePropertyChange("id", oldId, id);
    }

    @XmlTransient
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        String oldTitle = this.title;
        this.title = title;
        changeSupport.firePropertyChange("title", oldTitle, title);
    }

    @XmlTransient
    public Date getDuration() {
        return duration;
    }

    public void setDuration(Date duration) {
        Date oldDuration = this.duration;
        this.duration = duration;
        changeSupport.firePropertyChange("duration", oldDuration, duration);
    }

    @XmlTransient
    public int getTrackNr() {
        return trackNr;
    }

    public void setTrackNr(int trackNr) {
        int oldTrackNr = this.trackNr;
        this.trackNr = trackNr;
        changeSupport.firePropertyChange("trackNr", oldTrackNr, trackNr);
    }

    @XmlTransient
    public Collection<Playlist> getPlaylistCollection() {
        return playlistCollection;
    }

    public void setPlaylistCollection(Collection<Playlist> playlistCollection) {
        this.playlistCollection = playlistCollection;
    }

    @XmlTransient
    public Album getAlbumId() {
        return albumId;
    }

    public void setAlbumId(Album albumId) {
        Album oldAlbumId = this.albumId;
        this.albumId = albumId;
        changeSupport.firePropertyChange("albumId", oldAlbumId, albumId);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Song)) {
            return false;
        }
        Song other = (Song) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return String.format("%s [%d:%d]", title, duration.getMinutes(), duration.getSeconds());
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }
    
}
